// Fill out your copyright notice in the Description page of Project Settings.


#include "Character/StarrySkyCharacter.h"

#include "AbilitySystemComponent.h"
#include "AbilitySystem/StarrySkyAbilitySystemComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Play/StarrySkyPlayerController.h"
#include "Play/StarrySkyPlayerState.h"
#include "UI/HUD/StarrySkyHUD.h"

AStarrySkyCharacter::AStarrySkyCharacter()
{
	//角色移动设置
	//GetCharacterMovement()->bOrientRotationToMovement = true;//设置了 UCharacterMovementComponent 的 bOrientRotationToMovement 属性为 true，通常用于控制角色的旋转是否应该跟随移动方向
	GetCharacterMovement()->RotationRate = FRotator(0.f, 400.f, 0.f);//设置了 UCharacterMovementComponent 的RotationRate属性使得角色在 Y 轴上以每秒 400 度的速度旋转。这通常用于控制角色的旋转速度。
	GetCharacterMovement()->bConstrainToPlane = true;//设置了 UCharacterMovementComponent 的 bConstrainToPlane 属性为 true，这意味着角色的移动将被限制在一个平面上。这通常用于实现角色只能在水平面上移动，而不能垂直移动。
	GetCharacterMovement()->bSnapToPlaneAtStart = true;//设置了 UCharacterMovementComponent 的 bSnapToPlaneAtStart 属性为 true，这意味着角色在开始移动时会被强制放置在一个平面上。这通常用于确保角色始终位于地面或其他平面上，防止角色在游戏开始时悬空或不在预期的位置上
	//将角色自身的旋转控制设置未false,当你设置这些属性为 false 时，角色的旋转将不会受到控制器的影响
	bUseControllerRotationPitch = false;
	bUseControllerRotationYaw = false;
	bUseControllerRotationRoll = false;

	
	//UCharacterMovementComponent* MovementComp = GetCharacterMovement();
	//MovementComp->SetMovementMode(MOVE_Custom);  // 或者保持 Walking 但让根骨骼覆盖
	//MovementComp->bOrientRotationToMovement = false; // 让动画控制旋转
	//MovementComp->bUseControllerDesiredRotation = true;
}

void AStarrySkyCharacter::PossessedBy(AController* NewController)
{
	Super::PossessedBy(NewController);
	//服务器中初始化角色的AbilityActorInfo,设置玩家角色类中的AbilitySystemComponent和AttributeSet
	InitAbilityActorInfo();
	AddCharacterAbilities();
}

void AStarrySkyCharacter::OnRep_PlayerState()
{
	Super::OnRep_PlayerState();
	//客户端中初始化角色的AbilityActorInfo,设置玩家角色类中的AbilitySystemComponent和AttributeSet
	InitAbilityActorInfo();
}

int32 AStarrySkyCharacter::GetPlayerLevel()
{
	const AStarrySkyPlayerState* AuraPlayerState=GetPlayerState<AStarrySkyPlayerState>();
	check(AuraPlayerState);
	return AuraPlayerState->GetPlayerLevel();
}

void AStarrySkyCharacter::InitAbilityActorInfo()
{
	//从玩家状态中获取AbilitySystemComponent和AttributeSet
	AStarrySkyPlayerState* StarrySkyPlayerState=GetPlayerState<AStarrySkyPlayerState>();
	check(StarrySkyPlayerState);
	StarrySkyPlayerState->GetAbilitySystemComponent()->InitAbilityActorInfo(StarrySkyPlayerState,this);
	//游戏能量系统组件的添加委托绑定，并且施加效果的委托
	Cast<UStarrySkyAbilitySystemComponent>(StarrySkyPlayerState->GetAbilitySystemComponent())->AbilityActorInfoSet();
	AbilitySystemComponent=StarrySkyPlayerState->GetAbilitySystemComponent();
	AttributeSet=StarrySkyPlayerState->GetAttributeSet();
	//初始化默认属性
	InitializeDefaultAttributes();
	if(AStarrySkyPlayerController* StarrySkyPlayerController=GetController<AStarrySkyPlayerController>())
	{
		if(AStarrySkyHUD* StarrySkyHUD=Cast<AStarrySkyHUD>(StarrySkyPlayerController->GetHUD()))
		{
			StarrySkyHUD->InitOverlay(StarrySkyPlayerController,StarrySkyPlayerState,AbilitySystemComponent,AttributeSet);
		}
	}
}
